/**
 * Created by 刘玉波 on 2016/1/12.
 */
define(['angular', 'app.services'],function(angular, services){
    'use strict';

    services.service('User', ['$resource','$q','CacheManager', function ($resource,$q,CacheManager) {
        /**
         * 登陆
         */
        this.login = function(param,success,error){
            $resource("/login/do",param).save(null,success, error);
        };
        /**
         * 获取当前登录用户
         * @returns {*}
         */
      /*  this.getCurrentUser = function(){
            return $q(function(resolve, reject){

                var key = "/cu";
                var error = function(errXhr){
                    reject(errXhr);
                };
                $resource(key,null).get(function(jsonData){
                    resolve(jsonData);
                },error);

            });
        };*/

        this.getCurrentUser = function(){
            return $q(function(resolve, reject){
                var key = "/api/user";
                var success = function(jsonData){
                    //加入缓存
                    CacheManager.User[key] = jsonData;
                    resolve(jsonData);
                };
                var error = function(errXhr){
                    reject(errXhr);
                };
                var obj = CacheManager.User[key];
                if(obj){
                    resolve(obj);
                }else{
                    $resource(key,null).get(success,error);
                }
            });
        };


    }]);


});